package leetcode.easy.q03;

public class Main {
    public static void main(String[] args) {
        System.out.println(new Solution().lengthOfLongestSubstring("asdfghjkl;ASDFGHJKLasdfghj"));
    }
}

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.isEmpty()) return 0;
        if (s.length() == 1) return 1;
        int len[] = new int[s.length()];
        len[0] = 1;
        int max = len[0];
        for (int i = 1; i < s.length(); i++) {
            int first = findPos(s, s.charAt(i), i - len[i - 1]);
            if (first >= i) {
                len[i] = len[i - 1]+1;
                //max只会出现在这个场景，else都是在子串开找到重复，只会更短
                max = Math.max(max, len[i]);
            } else len[i] = i - first;

        }
        return max;

    }

    int findPos(String s, char ch, int start) {
        return s.indexOf(ch, start);
    }
}